package com.study.client.paho.service.impl;


import com.study.client.common.util.IotUtil;
import com.study.client.paho.ClientMQTT;
import com.study.client.paho.service.MqttClientService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.data.redis.core.StringRedisTemplate;

@Service
@Slf4j
public class MqttClientServiceImpl implements MqttClientService {

   /* final DataSourcesService dataSourcesService;

    @Autowired
    public MqttClientServiceImpl(DataSourcesService dataSourcesService) {
        this.dataSourcesService = dataSourcesService;
    }*/

/*    @Autowired
    private EmqxUtils emqxUtils;*/
    @Autowired
    StringRedisTemplate redisTemplate;

    @Value("${demp.datasource.mqtt.address}")
    private String mqttAddress;
    @Value("${demp.datasource.mqtt.username}")
    private String mqttUsername;
    @Value("${demp.datasource.mqtt.password}")
    private String mqttPassword;
    @Value("${demp.datasource.mqtt.dashboard.address}")
    private String mqttDashBoardAddress;
    @Value("${demp.datasource.mqtt.dashboard.username}")
    private String mqttDashBoardUsername;
    @Value("${demp.datasource.mqtt.dashboard.password}")
    private String mqttDashBoardPassword;
    @Value("${demp.datasource.mqtt.client.num}")
    private int clientNum;

    @Override
    public boolean addMqttClientAndSubscribe() {

        for (int i = 0; i < clientNum; i++) {
            try {
                String clientId = mqttUsername.replace('-', '@') + "@" + IotUtil.getHostName() + i;
                MqttClient client = ClientMQTT.createClientMQTT(mqttAddress, mqttUsername, mqttPassword, clientId);
                log.info("mqtt 连接成功");
            } catch (MqttException e) {
                log.error("连接mqtt失败:{}", e.toString());
            }
        }
        return false;
    }

    @Override
    public void mqttRegister() {
        //添加mqtt账号 stringRedisTemplate.opsForValue().
        redisTemplate.opsForHash().put("mqtt_user:ems-subscribe1", "password", "1234567");
        redisTemplate.opsForHash().put("mqtt_user:ems-client1", "password", "123456");
        redisTemplate.opsForHash().put("mqtt_user:ems-manager", "password", "123456");
        //添加mqtt权限
        redisTemplate.opsForHash().put("mqtt_acl:ems-subscribe1", "#", "3");
        redisTemplate.opsForHash().put("mqtt_acl:ems-manager", "#", "3");
        redisTemplate.opsForHash().put("mqtt_acl:ems-client1", "ems/+/gateway/register/reply", "1");
        redisTemplate.opsForHash().put("mqtt_acl:ems-client1", "ems/gateway/register", "2");
        redisTemplate.opsForHash().put("mqtt_acl:ems-client1", "ems/+/equipment/register", "2");
        redisTemplate.opsForHash().put("mqtt_acl:ems-client1", "ems/+/data/upload", "2");
        redisTemplate.opsForHash().put("mqtt_acl:ems-client1", "ems/+/warning/upload", "2");
        /**
         * 新增数据下发及告警反馈redis设置
         */
        redisTemplate.opsForHash().put("mqtt_acl:ems-client1", "ems/assign/+", "1");
        redisTemplate.opsForHash().put("mqtt_acl:ems-client1", "ems/feedback/+", "1");
        /**
         * 新设备对接
         */
        redisTemplate.opsForHash().put("mqtt_acl:ems-client1", "ems/ankerui/+", "2");
        redisTemplate.opsForHash().put("mqtt_acl:ems-client1", "ems/sixin/+", "2");
    }
}

